<?php
/**
 * 视图基类
 */
class View
{
    protected $variables = array();
    protected $_controller;
    protected $_action;

    function __construct($controller, $action)
    {
        $this->_controller = $controller;
        $this->_action = $action;
        //添加include 模版
        $this->compare_pattern[] = '#\{{include (.*?)\}}#';
        //简单的key value赋值
        $this->compare_pattern[] = '#\{{\\$(.*?)\}}#';
        //if条件语句实现
        $this->compare_pattern[] = '#\{{if\((.*?)\)\}}#';
        $this->compare_pattern[] = '#\{{elseif\((.*?)\)\}}#';
        $this->compare_pattern[] = '#\{{else\((.*?)\)\}}#';
        $this->compare_pattern[] = '#\{{/if\}}#';
        //foreach实现
        $this->compare_pattern[] = '#\{{foreach \(\\$(.*?) as (.*?)=>(.*?)\)\}}#';
        $this->compare_pattern[] = '#\{{/foreach\}}#';
        //支持原生php语言实现
        $this->compare_pattern[] = '#\{{php (.*?)\}}#';
        $this->compare_pattern[] = '#\{{/php\}}#';


        $this->compare_destpattern[] = "<?php \$include_data_contents = file_get_contents(APP_PATH.'app/views/\\1'); ?>";

        //以下是上面几个模版编译后的php语言实现
        $this->compare_destpattern[] = "<?php echo $\\1;?>";

        $this->compare_destpattern[] = "<?php if(\\1){ ?>";
        $this->compare_destpattern[] = "<?php }else if(\\1){ ?>";
        $this->compare_destpattern[] = "<?php }else{ ?>";
        $this->compare_destpattern[] = "<?php }?>";
        $this->compare_destpattern[] = "<?php foreach(\$\\1 as \\2 => \\3){?>";
        $this->compare_destpattern[] = "<?php } ?>";

        $this->compare_destpattern[] = "<?php \\1 ";
        $this->compare_destpattern[] = "?>";



    }
    /** 分配变量 **/
    function assign($name, $value)
    {
        $this->variables[$name] = $value;
    }
    /** 渲染显示 **/
    function render()
    {
        $is_header = 0;
        $is_footer = 0;
        extract($this->variables);

        $file_name = md5($this->_controller.'_'.$this->_action);
        $file_path = APP_PATH.'runtime/'.$file_name.'.php';
        if(!file_exists($file_path) || DEBUG==true) {

            // 页内容文件
            $content = file_get_contents(APP_PATH . 'app/views/' . $this->_controller . '/' . $this->_action . '.php');
            $compile_content = preg_replace($this->compare_pattern,$this->compare_destpattern,$content);

            $header = '';
            if($is_header) {
                if(file_exists(APP_PATH . 'app/views/base/'.HEADER_NAME)) {
                    $header = file_get_contents(APP_PATH . 'app/views/base/'.HEADER_NAME);
                    $header = preg_replace($this->compare_pattern,$this->compare_destpattern,$header);
                } else {
                    exit('NOT found header');
                }
            }

            $footer = '';
            if($is_footer) {
                if(file_exists(APP_PATH . 'app/views/base/'.FOOTER_NAME)) {
                    $footer = file_get_contents(APP_PATH . 'app/views/base/'.FOOTER_NAME);
                    $footer = preg_replace($this->compare_pattern,$this->compare_destpattern,$footer);
                } else {
                    exit('NOT found footer');
                }
            }

            file_put_contents($file_path,$header.$compile_content.$footer);
        }
        include_once $file_path;

    }
}